package com.py.payment.tools.dtu;

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class CharacterConvertUtils {

    /**
     * 异或运算
     * @param strHex_X
     * @param strHex_Y
     * @return
     */
    public static String xor(String strHex_X, String strHex_Y){
        //将x、y转成二进制形式
        String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));
        String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));
        String result = "";
        //判断是否为8位二进制，否则左补零
        if(anotherBinary.length() != 8){
            for (int i = anotherBinary.length(); i <8; i++) {
                anotherBinary = "0"+anotherBinary;
            }
        }
        if(thisBinary.length() != 8){
            for (int i = thisBinary.length(); i <8; i++) {
                thisBinary = "0"+thisBinary;
            }
        }
        //异或运算
        for(int i=0;i<anotherBinary.length();i++){
            //如果相同位置数相同，则补0，否则补1
            if(thisBinary.charAt(i)==anotherBinary.charAt(i))
                result+="0";
            else{
                result+="1";
            }
        }
        return Integer.toHexString(Integer.parseInt(result, 2)).toUpperCase();
    }

    public static String parseAsciiToString(Integer[] asciiArray) {
        byte[] bytes = new byte[asciiArray.length];
        for (int i = 0; i < asciiArray.length; i++) {
            bytes[i] = (byte) (asciiArray[i] & 0xFF);
        }
        String asciiStr = new String(bytes, StandardCharsets.US_ASCII);
        return asciiStr; // 返回解析后的字符串
    }

}
